const path = require('path')
const fs = require('fs')

const getFileSize = (pathname) => {
  let fileSize = 0
  const _temp = (pathname) => {
    try {
      let absPath = path.resolve(pathname)
      const statObj = fs.statSync(absPath)
      if (statObj.isFile()) {
        const file = fs.readFileSync(absPath)
        fileSize += file.length
        console.log('==file', absPath, '\t', file.length)
      } else {
        const dirs = fs.readdirSync(absPath)
        dirs.map((dir) => {
          _temp(path.join(absPath, dir))
        })
      }
    } catch (e) {
      console.log('==e', e)
    }
  }
  _temp(pathname)
  return fileSize
}

const totalFileSize = getFileSize('/Users/jieyang/Desktop')
console.log('==totalFileSize', `${totalFileSize} byte`)
